package com.fmdj.dr.service.impl;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.fmdj.common.exception.GlobalException;
import com.fmdj.common.util.MicroAppUtil;
import com.fmdj.common.util.PageUtils;
import com.fmdj.dr.db.dao.DriverDao;
import com.fmdj.dr.db.dao.DriverSettingsDao;
import com.fmdj.dr.db.dao.WalletDao;
import com.fmdj.dr.db.pojo.DriverSettingsEntity;
import com.fmdj.dr.db.pojo.WalletEntity;
import com.fmdj.dr.service.DriverService;
import com.fmdj.dr.service.DriverSettingsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@Service
@Slf4j
public class DriverServiceImpl implements DriverService {

    @Autowired
    private DriverDao driverDao;

    @Autowired
    private MicroAppUtil microAppUtil;

    @Autowired
    private DriverSettingsDao driverSettingsDao;

    @Autowired
    private WalletDao walletDao;

    @Transactional
    @LcnTransaction
    @Override
    public String registerDriver(Map map) {
        //根据登陆凭证获取openId
        String openId = microAppUtil.getOpenId(MapUtil.getStr(map, "code"));

        //根据openId查询司机是否存在
        HashMap<Object, Object> params = new HashMap<>(){{
            put("openId", openId);
        }};
        long driver = driverDao.existsDriver(params);
        if (driver!=0) {
            throw new GlobalException("该司机已注册");
        }

        //保存司机信息
        map.put("openId", openId);
        driverDao.registerDriver(map);

        //获取司机id
        String driverId = driverDao.getDriverId(openId);

        //创建司机设置信息的实体
        DriverSettingsEntity settings = new DriverSettingsEntity();
        settings.setDriverId(Long.parseLong(driverId));

        //保存司机设置信息 创建JSONObject对象 因为数据库中保存的是JSON字符串
        JSONObject jsonObject = new JSONObject(); //用于存储json数据
        jsonObject.set("autoAccept",false);
        jsonObject.set("orientation","");
        jsonObject.set("orderDistance",0);
        jsonObject.set("rangeDistance",5);
        jsonObject.set("listenService",true);
        settings.setSettings(jsonObject.toString());
        driverSettingsDao.insertDriversSettings(settings);

        //保存司机钱包设置
        WalletEntity walletEntity = new WalletEntity();
        walletEntity.setDriverId(Long.parseLong(driverId));
        walletEntity.setBalance(BigDecimal.valueOf(0));
        walletEntity.setPassword(null);
        walletDao.insert(walletEntity);

        return driverId;
    }

    @Override
    @Transactional
    @LcnTransaction
    public int updateDriverAuth(Map map) {
        return driverDao.updateDriverAuth(map);
    }

    //查询不需要事务管理以及分布式事务管理
    @Override
    public HashMap login(String code) {
        //根据登陆凭证获取openId
        String openId = microAppUtil.getOpenId(code);
        HashMap loginResult = driverDao.login(openId);

        //判断登陆结果
        if (loginResult!=null) {
            //判断司机是否已经实名认证
            if (loginResult.containsKey("archive")) {
                //因为前端需要boolean  类型 所以这里需要转换
                Integer archive = MapUtil.getInt(loginResult, "archive");
                Boolean arc = (archive == 1 ? true : false);
                loginResult.replace("archive", arc); //将转换后的数据替换掉
            }
        }
        return loginResult;
    }

    @Override
    public HashMap selectDriverInfo(long driverId) {
        HashMap hashMap = driverDao.selectDriverInfo(driverId);

        //将从数据库中取出的summary字段转换成json对象  这里不能转成json数组 以为数组要以[ 开头
        JSONObject summary = JSONUtil.parseObj(MapUtil.getStr(hashMap, "summary"));

        //将hashMap中的summary字段替换成json对象
        hashMap.replace("summary", summary);

        return hashMap;
    }

    @Override
    public HashMap selectDriverInfoById(Long driverId) {
        return driverDao.selectDriverInfoById(driverId);
    }

    @Override
    public PageUtils selectDriverByPage(Map map) {
        //计算起始索引
        Integer length = MapUtil.getInt(map, "length");
        Integer page = MapUtil.getInt(map, "page");
        int start = (page - 1) * length;
        map.put("start", start);


        long count = driverDao.selectDriverByCount(map);
        ArrayList<HashMap> list = driverDao.selectDriverByPage(map);

        return new PageUtils(list, count, page, length);
    }

    @Override
    public HashMap selectDriverAuthInfo(Long driverId) {
        return driverDao.selectDriverAuthInfo(driverId);
    }

    @Override
    public int updateDriverRealAuth(Map param) {
        return driverDao.updateDriverRealAuth(param);
    }

    @Override
    public HashMap searchDriverBriefInfo(long driverId) {
        HashMap hashMap = driverDao.searchDriverBriefInfo(driverId);
        return hashMap;
    }

    @Override
    public String searchDriverOpenId(long driverId) {
        return driverDao.searchDriverOpenId(driverId);
    }
}
